package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.pojo.NursingLevel;
import com.zzyl.nursing.domain.vo.NursingLevelVo;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.service.INursingLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 护理等级Service业务层处理
 *
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    private static final String CACHE_KEY_PREFIX = "nursingLevel:all";

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {
        return nursingLevelMapper.selectById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        int inserted = nursingLevelMapper.insert(nursingLevel);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return inserted;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        int updated = nursingLevelMapper.updateById(nursingLevel);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return updated;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        return nursingLevelMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        int deleted = nursingLevelMapper.deleteById(id);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return deleted;
    }

    /**
     * 查询护理等级Vo列表
     *
     * @param nursingLevel 条件
     * @return 结果
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelVoList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelVoList(nursingLevel);
    }

    /**
     * 查询所有护理等级
     */
    @Override
    public List<NursingLevelVo> listAll() {
        //1.注入redis模块
        ValueOperations<Object, Object> ops = redisTemplate.opsForValue();
        //2.在redis进行查询,强转为List<NursingLevelVo>对象
        List<NursingLevelVo> nursingLevelVoList = (List<NursingLevelVo>) ops.get(CACHE_KEY_PREFIX);
        //3.判断该对象是否为空,也就是判断该对象在redis内存中是否存在
        if (CollectionUtils.isEmpty(nursingLevelVoList)) {
            //3.1 redis中该对象为空,所以要从数据库查询后并添加到redis中
            List<NursingLevel> list = this.list(Wrappers.lambdaQuery(NursingLevel.class).eq(NursingLevel::getStatus, 1));
            nursingLevelVoList = BeanUtil.copyToList(list, NursingLevelVo.class);
            ops.set(CACHE_KEY_PREFIX, nursingLevelVoList);
        }
        //4.不为空,也就是redis中存在,直接从redis中获取
        return nursingLevelVoList;
    }
}
